local extension = Package:new("huoying")
extension.extensionName = "decade"
Fk:loadTranslationTable{
    ["huoying"] = "火影",
}

-- 创建波风水门武将
local shuimen = General:new(extension, "huoying__shuimen", "god", 4, 4)

-- 添加武将翻译
Fk:loadTranslationTable{
  ["huoying__shuimen"] = "波风水门",
  ["#huoying__shuimen"] = "金色闪光",
  ["designer:huoying__shuimen"] = "decade",
  ["cv:huoying__shuimen"] = "无",
  ["illustrator:huoying__shuimen"] = "无",
}

-- 创建飞雷神技能
local huoying_feilei = fk.CreateTriggerSkill{
  name = "huoying_feilei",
  anim_type = "control",
  events = {fk.TargetSpecified},
  
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and 
           data.to and data.to ~= player.id and player:getHandcardNum() > 0
  end,

  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name)
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    local target_player = room:getPlayerById(data.to)
    
    -- 选择一张手牌
    local cards = room:askForCard(player, 1, 1, true, self.name, true, ".", "#huoying_feilei-give")
    if #cards > 0 then
      -- 弃置一张手牌
      room:throwCard(cards, self.name, player, player)
      -- 添加标记到目标角色
      room:addPlayerMark(target_player, "@feilei", 1)
      player:broadcastSkillInvoke(self.name)
    end
  end,
}

-- 创建飞雷神防御效果
local huoying_feilei_prohibit = fk.CreateProhibitSkill{
  name = "#huoying_feilei_prohibit",
  is_prohibited = function(self, from, to, card)
    -- 如果是自己使用牌，不触发禁止效果
    if from == to then return false end
    -- 如果目标不是拥有飞雷神技能的角色，不触发禁止效果
    if not to or not to:hasSkill("huoying_feilei") then return false end
    -- 检查目标是否在攻击范围外
    return not from:inMyAttackRange(to)
  end,
}

huoying_feilei:addRelatedSkill(huoying_feilei_prohibit)

-- 创建飞雷神二段技能
local huoying_feilei_effect = fk.CreateActiveSkill{
  name = "huoying_feilei_effect",
  anim_type = "support",
  can_use = function(self, player)
    return player.phase == Player.Play
  end,
  
  card_filter = function(self, to_select, selected)
    return false
  end,

  card_num = 0,

  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and 
           Fk:currentRoom():getPlayerById(to_select):getMark("@feilei") > 0
  end,

  target_num = 1,

  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    
    -- 广播技能发动
    player:broadcastSkillInvoke("huoying_feilei")
    room:sendLog{type = "#huoying_feilei_effect", from = player.id, to = {target.id}}
    
    -- 移除飞雷神标记
    room:setPlayerMark(target, "@feilei", target:getMark("@feilei") - 1)
    
    -- 摸两张牌
    room:drawCards(player, 2, self.name)
    
    -- 询问是否使用手牌
    if player:getHandcardNum() > 0 then
      local prompt = "#huoying_feilei_use:" .. target.id
      if room:askForSkillInvoke(player, self.name, { to = target.id }, prompt) then
        -- 选择一张手牌使用
        local cards = room:askForCard(player, 1, 1, true, self.name, true, nil, "@huoying_feilei_select")
        if #cards > 0 then
          local card = Fk:getCardById(cards[1])
          if card then
            card.skillName = self.name
            room:useCard({
              from = player.id,
              tos = {{target.id}},
              card = card,
              prohibitedCardNames = {"jink", "nullification"},  -- 此牌不可响应
            })
            room:sendLog{type = "#huoying_feilei_usecard", from = player.id, to = {target.id}, 
              arg = card:toLogString(),
            }
          end
        end
      end
    end
  end,
}

-- 创建导雷技能
local huoying_daolei = fk.CreateTriggerSkill{
  name = "huoying_daolei",
  anim_type = "defensive",
  events = {fk.DamageInflicted},
  
  can_trigger = function(self, event, target, player, data)
    -- 检查是否是技能拥有者受到伤害
    if target ~= player or not player:hasSkill(self.name) then return false end
    
    -- 检查是否有其他角色有飞雷神标记
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p:getMark("@feilei") > 0 then
        return true
      end
    end
    return false
  end,

  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    -- 获取所有有飞雷神标记的其他角色
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p:getMark("@feilei") > 0 then
        table.insert(targets, p.id)
      end
    end
    
    if #targets > 0 then
      local to = room:askForChoosePlayers(player, targets, 1, 1, "@huoying_daolei-target", self.name, true)
      if #to > 0 then
        self.cost_data = to[1]
        return true
      end
    end
    return false
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    
    -- 移除飞雷神标记
    room:setPlayerMark(to, "@feilei", to:getMark("@feilei") - 1)
    -- 摸两张牌
    room:drawCards(player, 2, self.name)
    -- 转移伤害
    local damage = table.simpleClone(data)
    damage.to = to
    room:damage(damage)
    -- 取消原伤害
    return true
  end,
}

-- 创建互瞬回旋技能
local huoying_hushun = fk.CreateActiveSkill{
  name = "huoying_hushun",
  anim_type = "support",
  can_use = function(self, player)
    return player.phase == Player.Play
  end,
  
  card_filter = function(self, to_select, selected)
    return false
  end,

  card_num = 0,

  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and 
           Fk:currentRoom():getPlayerById(to_select):getMark("@feilei") > 0
  end,

  target_num = 1,

  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    
    -- 移除飞雷神标记
    room:setPlayerMark(target, "@feilei", target:getMark("@feilei") - 1)
    -- 添加互瞬回旋标记
    room:setPlayerMark(target, "@hushun", 1)
    
    -- 广播技能发动
    player:broadcastSkillInvoke(self.name)
    room:sendLog{type = "#huoying_hushun", from = player.id, to = {target.id}}
  end,
}

-- 创建互瞬回旋效果
local huoying_hushun_effect = fk.CreateTriggerSkill{
  name = "#huoying_hushun_effect",
  mute = true,
  events = {fk.CardUseFinished},
  
  can_trigger = function(self, event, target, player, data)
    -- 检查是否是【杀】
    if not data.card or data.card.name ~= "slash" then return false end
    -- 检查是否是技能拥有者使用的
    if target ~= player or not player:hasSkill("huoying_hushun") then return false end
    -- 检查是否有角色有互瞬回旋标记
    for _, p in ipairs(player.room:getOtherPlayers(player)) do
      if p:getMark("@hushun") > 0 then return true end
    end
    return false
  end,

  on_cost = function() return true end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 获取有互瞬回旋标记的角色
    local hushun_player
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p:getMark("@hushun") > 0 then
        hushun_player = p
        break
      end
    end
    
    if hushun_player then
      -- 移除互瞬回旋标记
      room:setPlayerMark(hushun_player, "@hushun", 0)
      -- 获取【杀】的目标
      local to = room:getPlayerById(data.tos[1][1])
      if to and not to.dead then
        -- 让有标记的角色也使用一张【杀】
        local slash = Fk:cloneCard("slash")
        slash.skillName = self.name
        room:useCard({
          from = hushun_player.id,
          tos = {{to.id}},
          card = slash,
        })
      end
    end
  end,
}

huoying_hushun:addRelatedSkill(huoying_hushun_effect)

-- 添加技能
shuimen:addSkill(huoying_feilei)
shuimen:addSkill(huoying_feilei_effect)
shuimen:addSkill(huoying_daolei)
shuimen:addSkill(huoying_hushun)

-- 添加技能翻译
Fk:loadTranslationTable{
  ["huoying_feilei"] = "飞雷神",
  [":huoying_feilei"] = "你使用牌指定其他角色后，可以弃置一张手牌，为其添加一枚飞雷神标记。当你不在其他角色攻击范围内时，其无法使用牌指定你为目标。",
  ["huoying_feilei_effect"] = "二段",
  [":huoying_feilei_effect"] = "出牌阶段，你可以指定一名有飞雷神标记的角色，移除其一枚飞雷神标记并摸两张牌，然后你可以对其使用一张手牌（不可响应）。",
  ["huoying_daolei"] = "导雷",
  [":huoying_daolei"] = "当你即将受到伤害时，你可以选择一名武将牌上有飞雷神标记的其他角色，弃置其武将牌上一枚飞雷神标记，将伤害转移给该角色并摸两张牌。",
  ["huoying_hushun"] = "互瞬",
  [":huoying_hushun"] = "出牌阶段，你可以选择一名有飞雷神标记的其他角色，弃置其一枚标记。你使用的下一张【杀】，该角色视为对同一目标也使用此牌。",
  ["@huoying_daolei-target"] = "导雷：请选择一名有飞雷神标记的角色，将伤害转移给该角色",
  ["#huoying_feilei-invoke"] = "飞雷神：你可以弃置一张手牌，为目标角色添加一枚飞雷神标记",
  ["#huoying_feilei-give"] = "请选择一张手牌弃置",
  ["#huoying_feilei_addMark"] = "%from 发动了飞雷神，为 %to 添加了一枚飞雷神标记",
  ["#huoying_feilei_effect"] = "%from 发动了飞雷神二段，移除了 %to 的一枚飞雷神标记并摸了两张牌",
  ["#huoying_feilei_use"] = "是否使用一张手牌？",
  ["#huoying_feilei_usecard"] = "%from 对 %to 使用了一张 %arg（此牌不可响应）",
  ["#huoying_hushun"] = "%from 发动了互瞬回旋，移除了 %to 的一枚飞雷神标记",
  ["@huoying_feilei_select"] = "请选择一张手牌对目标角色使用",
  ["@huoying_feilei_target"] = "请选择一名有飞雷神标记的角色",
  ["@feilei"] = "飞雷神",
  ["@hushun"] = "互瞬"
}

return extension